package com.JaMorant.SSM.thc.listener;

import com.JaMorant.SSM.model.thc.GoodsConfig;
import com.JaMorant.SSM.model.thc.PddOrder;
import com.JaMorant.SSM.thc.mapper.PddOrderMapper;
import com.JaMorant.SSM.vo.thc.GoodsConfigVo;
import com.JaMorant.SSM.vo.thc.PddOrderVo;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import java.math.BigDecimal;
import java.time.Instant;

@Component
public class PddOrderListener extends AnalysisEventListener<PddOrderVo> {

    //注入mapper
    @Autowired
    private PddOrderMapper pddOrderMapper;

    //一行一行，从第二行
    @Override
    public void invoke(PddOrderVo pddOrderVo, AnalysisContext analysisContext) {
        PddOrder pddOrder = new PddOrder();
        BeanUtils.copyProperties(pddOrderVo,pddOrder);


        if(pddOrderVo == null || StringUtils.isBlank(pddOrderVo.getOrderNo() )){
            pddOrder.setOrderNo(DigestUtils.md5DigestAsHex((pddOrderVo.getRemark()+ Instant.now()).getBytes()));
        }
        BigDecimal bigDecimal = new BigDecimal(0);
        if(pddOrderVo.getInPrice() != null && pddOrderVo.getInPrice().compareTo(bigDecimal) >0){
            pddOrder.setPrice(pddOrderVo.getInPrice());
        } else {
            pddOrder.setPrice(pddOrderVo.getOutPrice());
        }

        //添加
        pddOrderMapper.insert(pddOrder);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
